/**
 * Created by 14229 on 2020/2/19.
 */
import java.util.HashMap;
import java.util.Map;

public class Test09 {
    public static void main(String[] args) {

        String s1 = "{'name':'张同学'}";
        String s2 = "{'name':'张同学','age':20}";
        String s3 = "{'name':'张同学','age':20,'birthday':'1999-10-06'}";

        System.out.println("------- 字符串S1解析结果如下 -------");
        Map<String, String> map1 = parseProperties(s1);
        for(String key:map1.keySet()){
            System.out.println("key = "+key+" ,value = "+map1.get(key));
        }

        System.out.println("------- 字符串S2解析结果如下 -------");
        Map<String, String> map2 = parseProperties(s2);
        for(String key:map2.keySet()){
            System.out.println("key = "+key+" ,value = "+map2.get(key));
        }

        System.out.println("------- 字符串S3解析结果如下 -------");
        Map<String, String> map3 = parseProperties(s3);
        for(String key:map3.keySet()){
            System.out.println("key = "+key+" ,value = "+map3.get(key));
        }

    }

    /**
     * @param str
     * @return
     */
    public static Map<String,String> parseProperties(String str){
        //补全代码实现字符串解析
        Map<String,String> map =  new HashMap<>();
        String[] replace = str.replace("{", "").replace("'", "").replace("}", "").split(",");
        for (String s : replace) {
            String[] split = s.split(":");
            map.put(split[0],split[1]);
        }
        return map;

    }

}
